<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>xmake v2.1.5版本新特性介绍</title>
    <meta name="description" content="2.1.5版本现已进入收尾阶段，此版本加入了一大波新特性，目前正在进行稳定性测试和修复，在这里，先来介绍下新版本中引入了哪些些新特性和改进。1. 提供类似cmake的find_*系列接口，实现各种查找，例如：find_package, find_library, find_file, ...2. 提供模块接口，实...">

    
    <meta name="keywords" content="xmake,lua,cmake,包查找,编译器特性检测,预编译头文件,扩展模块,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2017/07/29/new-features-v2.1.5/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>xmake v2.1.5版本新特性介绍</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2017-07-29
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#lua" title="Tag: lua" rel="tag">lua</a-->
        <a href="/cn/tag/#lua" title="Tag: lua" rel="tag">lua</a>&nbsp;
    
        <!--a href="/tag/#cmake" title="Tag: cmake" rel="tag">cmake</a-->
        <a href="/cn/tag/#cmake" title="Tag: cmake" rel="tag">cmake</a>&nbsp;
    
        <!--a href="/tag/#%E5%8C%85%E6%9F%A5%E6%89%BE" title="Tag: 包查找" rel="tag">包查找</a-->
        <a href="/cn/tag/#包查找" title="Tag: 包查找" rel="tag">包查找</a>&nbsp;
    
        <!--a href="/tag/#%E7%BC%96%E8%AF%91%E5%99%A8%E7%89%B9%E6%80%A7%E6%A3%80%E6%B5%8B" title="Tag: 编译器特性检测" rel="tag">编译器特性检测</a-->
        <a href="/cn/tag/#编译器特性检测" title="Tag: 编译器特性检测" rel="tag">编译器特性检测</a>&nbsp;
    
        <!--a href="/tag/#%E9%A2%84%E7%BC%96%E8%AF%91%E5%A4%B4%E6%96%87%E4%BB%B6" title="Tag: 预编译头文件" rel="tag">预编译头文件</a-->
        <a href="/cn/tag/#预编译头文件" title="Tag: 预编译头文件" rel="tag">预编译头文件</a>&nbsp;
    
        <!--a href="/tag/#%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97" title="Tag: 扩展模块" rel="tag">扩展模块</a-->
        <a href="/cn/tag/#扩展模块" title="Tag: 扩展模块" rel="tag">扩展模块</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>2.1.5版本现已进入收尾阶段，此版本加入了一大波新特性，目前正在进行稳定性测试和修复，在这里，先来介绍下新版本中引入了哪些些新特性和改进。</p>

<div class="highlighter-rouge"><pre class="highlight"><code>1. 提供类似cmake的find_*系列接口，实现各种查找，例如：find_package, find_library, find_file, ...
2. 提供模块接口，实现编译器的各种检测，例如：has_features, has_flags, has_cincludes, has_cfuncs, ...
3. 实现大量扩展模块，提供文件下载、解压缩、git操作等接口
4. 支持预编译头文件支持，改进c++编译效率
5. 支持在工程中自定义模块进行扩展
6. 提供代码片段检测接口，实现更加灵活定制化的检测需求
7. 改进option和target，提供更加动态化的配置
8. 通过find_package实现包依赖管理2.0版本
9. 改进root权限问题，实现更加安全的root下运行
10. 提供compile_commands.json导出插件
11. 改进vs201x工程生成插件，支持多模式、多架构同时构建和自由切换不干扰
</code></pre>
</div>

<h4 id="利用find_package查找依赖包">利用find_package查找依赖包</h4>

<p>此接口参考了cmake对于<code class="highlighter-rouge">find_*</code>系列接口的设计，实现在项目中动态的查找和添加包依赖。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>
    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.find_package"</span><span class="p">)</span>
        <span class="n">target</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="n">find_package</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">))</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>上述描述代码，通过<a href="http://xmake.io/#/zh/manual?id=detect-find_package">lib.detect.find_package</a>来查找包，如果找到<code class="highlighter-rouge">zlib</code>包，则将<code class="highlighter-rouge">links</code>, <code class="highlighter-rouge">includedirs</code>和<code class="highlighter-rouge">linkdirs</code>等信息添加到target中去。</p>

<h4 id="实现包管理20">实现包管理2.0</h4>

<p>2.1.4版本之前，xmake对于包管理，是通过在项目内置<code class="highlighter-rouge">pkg/zlib.pkg</code>方式，来检测链接的，虽然也支持自动检测，但是查找功能有限，并且内置的各个架构的二进制库到项目，对git并不是很友好。</p>

<p>现在通过<code class="highlighter-rouge">find_package</code>和<code class="highlighter-rouge">option</code>，我们可以实现更好的包管理：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">option</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">)</span>
    <span class="n">set_showmenu</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
    <span class="n">before_check</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">option</span><span class="p">)</span>
        <span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.find_package"</span><span class="p">)</span>
        <span class="n">option</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="n">find_package</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">))</span>
    <span class="k">end</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">add_options</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">)</span>
</code></pre>
</div>

<p>通过定义一个名为zlib的选项作为包，关联到target，在选项被检测之前，先从系统中查找zlib包，如果存在，则添加对应的<code class="highlighter-rouge">links</code>, <code class="highlighter-rouge">linkdirs</code>等配置信息，然后进行选项检测，如果选项检测通过，这个target在编译的时候就会启用zlib。</p>

<p>如果要手动禁用这个zlib包，使其不参与自动检测和链接，只需要：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f --zlib<span class="o">=</span>n 
<span class="gp">$ </span>xmake
</code></pre>
</div>

<p>注：2.2.1版本将会实现包管理3.0，更加自动化的依赖包管理和使用，具体详情见：<a href="https://github.com/xmake-io/xmake/issues/69">Remote package management</a>。</p>

<p>例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"mbedtls master optional"</span><span class="p">)</span>
<span class="n">add_requires</span><span class="p">(</span><span class="s2">"pcre2 &gt;=1.2.0"</span><span class="p">,</span> <span class="s2">"zlib &gt;= 1.2.11"</span><span class="p">)</span>
<span class="n">add_requires</span><span class="p">(</span><span class="s2">"git@github.com:glennrp/libpng.git@libpng &gt;=1.6.28"</span><span class="p">)</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">add_packages</span><span class="p">(</span><span class="s2">"pcre2"</span><span class="p">,</span> <span class="s2">"zlib"</span><span class="p">,</span> <span class="s2">"libpng"</span><span class="p">,</span> <span class="s2">"mbedtls"</span><span class="p">)</span>
</code></pre>
</div>

<p>目前正在努力开发中，尽情期待。。</p>

<h4 id="模块的自定义扩展">模块的自定义扩展</h4>

<p>我们可以通过在工程的<code class="highlighter-rouge">xmake.lua</code>文件的开头指定下扩展modules的目录：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_moduledirs</span><span class="p">(</span><span class="s2">"$(projectdir)/xmake/modules"</span><span class="p">)</span>
</code></pre>
</div>

<p>这样xmake就能找到自定义的扩展模块了，例如：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>projectdir
 - xmake
   - modules
     - detect/package/find_openssl.lua
</code></pre>
</div>

<p>通过在自定义的工程模块目录，添加一个<code class="highlighter-rouge">find_openssl.lua</code>的脚本，就可以扩展<code class="highlighter-rouge">find_package</code>，使得包查找更加精准。</p>

<p>这里顺便总结下，<code class="highlighter-rouge">find_package</code>的查找顺序：</p>

<ol>
  <li>如果指定<code class="highlighter-rouge"><span class="p">{</span><span class="err">packagedirs</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nt">""</span><span class="err">}</span></code>参数，优先从这个参数指定的路径中查找本地包<code class="highlighter-rouge">*.pkg</code></li>
  <li>如果在<code class="highlighter-rouge">xmake/modules</code>下面存在<code class="highlighter-rouge">detect.packages.find_xxx</code>脚本，那么尝试调用此脚本来改进查找结果</li>
  <li>如果系统存在<code class="highlighter-rouge">pkg-config</code>，并且查找的是系统环境的库，则尝试使用<code class="highlighter-rouge">pkg-config</code>提供的路径和链接信息进行查找</li>
  <li>如果系统存在<code class="highlighter-rouge">homebrew</code>，并且查找的是系统环境的库，则尝试使用<code class="highlighter-rouge">brew --prefix xxx</code>提供的信息进行查找</li>
  <li>从参数中指定的pathes路径和一些已知的系统路径<code class="highlighter-rouge">/usr/lib</code>, <code class="highlighter-rouge">/usr/include</code>中进行查找</li>
</ol>

<h4 id="快速判断编译器特性检测支持">快速判断编译器特性检测支持</h4>

<p>通过<code class="highlighter-rouge">core.tool.compiler</code>模块的<a href="http://xmake.io/#/zh/manual?id=compiler-has_features">compiler.has_features</a>接口，在<code class="highlighter-rouge">xmake.lua</code>中预先判断当前编译期支持的语言特性，实现条件编译。</p>

<p>此处也是参考了cmake的设计，具体详情见：<a href="https://github.com/xmake-io/xmake/issues/83">issues#83</a>。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="n">import</span><span class="p">(</span><span class="s2">"core.tool.compiler"</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">compiler</span><span class="p">.</span><span class="n">has_features</span><span class="p">(</span><span class="s2">"cxx_constexpr"</span><span class="p">)</span> <span class="k">then</span>
            <span class="n">target</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"defines"</span><span class="p">,</span> <span class="s2">"HAS_CXX_CONSTEXPR=1"</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>上述代码，在加载target的时候，判断当前编译器是否支持c++的常量表达式语法特性，如果支持则添加宏定义：<code class="highlighter-rouge">HAS_CXX_CONSTEXPR=1</code>。</p>

<p>我们也可以在判断时候，追加一些参数控制编译选项，例如上述特性需要<code class="highlighter-rouge">c++11</code>支持，我们可以启用它：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">compiler</span><span class="p">.</span><span class="n">has_features</span><span class="p">({</span><span class="s2">"c_static_assert"</span><span class="p">,</span> <span class="s2">"cxx_constexpr"</span><span class="p">},</span> <span class="p">{</span><span class="n">languages</span> <span class="o">=</span> <span class="s2">"cxx11"</span><span class="p">})</span> <span class="k">then</span>
    <span class="c1">-- ok</span>
<span class="k">end</span>
</code></pre>
</div>

<p>如果之前对这个target已经设置了<code class="highlighter-rouge">c++11</code>，那么我们也可以传入target对象，继承target的所有设置：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">compiler</span><span class="p">.</span><span class="n">has_features</span><span class="p">(</span><span class="s2">"cxx_constexpr"</span><span class="p">,</span> <span class="p">{</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span><span class="p">,</span> <span class="n">defines</span> <span class="o">=</span> <span class="s2">".."</span><span class="p">,</span> <span class="n">includedirs</span> <span class="o">=</span> <span class="s2">".."</span><span class="p">})</span> <span class="k">then</span>
    <span class="c1">-- ok</span>
<span class="k">end</span>
</code></pre>
</div>

<p>所有的c/c++编译器特性列表，见：<a href="http://xmake.io/#/zh/manual?id=compiler-features">compiler.features</a></p>

<h4 id="判断指定cc头文件是否存在">判断指定c/c++头文件是否存在</h4>

<p>通过<a href="http://xmake.io/#/zh/manual?id=detect-has_cincludes">lib.detect.has_cincludes</a>来检测c头文件是否存在。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.has_cincludes"</span><span class="p">)</span>

<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_cincludes</span><span class="p">(</span><span class="s2">"stdio.h"</span><span class="p">)</span>
<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_cincludes</span><span class="p">({</span><span class="s2">"stdio.h"</span><span class="p">,</span> <span class="s2">"stdlib.h"</span><span class="p">},</span> <span class="p">{</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span><span class="p">})</span>
<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_cincludes</span><span class="p">({</span><span class="s2">"stdio.h"</span><span class="p">,</span> <span class="s2">"stdlib.h"</span><span class="p">},</span> <span class="p">{</span><span class="n">defines</span> <span class="o">=</span> <span class="s2">"_GNU_SOURCE=1"</span><span class="p">,</span> <span class="n">languages</span> <span class="o">=</span> <span class="s2">"cxx11"</span><span class="p">})</span>
</code></pre>
</div>

<p>c++头文件的检测，见：<a href="http://xmake.io/#/zh/manual?id=detect-has_cxxincludes">lib.detect.has_cxxincludes</a></p>

<h4 id="判断指定cc函数是否存在">判断指定c/c++函数是否存在</h4>

<p>通过<a href="http://xmake.io/#/zh/manual?id=detect-has_cfuncs">lib.detect.has_cfuncs</a>来检测c函数是否存在。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.has_cfuncs"</span><span class="p">)</span>

<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_cfuncs</span><span class="p">(</span><span class="s2">"setjmp"</span><span class="p">)</span>
<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_cfuncs</span><span class="p">({</span><span class="s2">"sigsetjmp((void*)0, 0)"</span><span class="p">,</span> <span class="s2">"setjmp"</span><span class="p">},</span> <span class="p">{</span><span class="n">includes</span> <span class="o">=</span> <span class="s2">"setjmp.h"</span><span class="p">})</span>
</code></pre>
</div>

<p>c++函数的检测，见：<a href="http://xmake.io/#/zh/manual?id=detect-has_cxxfuncs">lib.detect.has_cxxfuncs</a>。</p>

<h4 id="判断指定cc类型是否存在">判断指定c/c++类型是否存在</h4>

<p>通过<a href="http://xmake.io/#/zh/manual?id=detect-has_ctypes">lib.detect.has_ctypes</a>来检测c函数是否存在。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.has_ctypes"</span><span class="p">)</span>

<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_ctypes</span><span class="p">(</span><span class="s2">"wchar_t"</span><span class="p">)</span>
<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_ctypes</span><span class="p">({</span><span class="s2">"char"</span><span class="p">,</span> <span class="s2">"wchar_t"</span><span class="p">},</span> <span class="p">{</span><span class="n">includes</span> <span class="o">=</span> <span class="s2">"stdio.h"</span><span class="p">})</span>
<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">has_ctypes</span><span class="p">(</span><span class="s2">"wchar_t"</span><span class="p">,</span> <span class="p">{</span><span class="n">includes</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"stdio.h"</span><span class="p">,</span> <span class="s2">"stdlib.h"</span><span class="p">},</span> <span class="s2">"defines = "</span><span class="n">_GNU_SOURCE</span><span class="o">=</span><span class="mi">1</span><span class="s2">", languages = "</span><span class="n">cxx11</span><span class="s2">"})
</span></code></pre>
</div>

<p>c++类型的检测，见：<a href="http://xmake.io/#/zh/manual?id=detect-has_cxxtypes">lib.detect.has_cxxtypes</a>。</p>

<h4 id="检测cc代码片段是否能够编译通过">检测c/c++代码片段是否能够编译通过</h4>

<p>通用的c/c++代码片段检测接口，通过传入多个代码片段列表，它会自动生成一个编译文件，然后常识对它进行编译，如果编译通过返回true。</p>

<p>对于一些复杂的编译器特性，连<a href="http://xmake.io/#/zh/manual?id=compiler-has_features">compiler.has_features</a>都无法检测到的时候，可以通过此接口通过尝试编译来检测它。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.check_cxsnippets"</span><span class="p">)</span>

<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">check_cxsnippets</span><span class="p">(</span><span class="s2">"void test() {}"</span><span class="p">)</span>
<span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">check_cxsnippets</span><span class="p">({</span><span class="s2">"void test(){}"</span><span class="p">,</span> <span class="s2">"#define TEST 1"</span><span class="p">},</span> <span class="p">{</span><span class="n">types</span> <span class="o">=</span> <span class="s2">"wchar_t"</span><span class="p">,</span> <span class="n">includes</span> <span class="o">=</span> <span class="s2">"stdio.h"</span><span class="p">})</span>
</code></pre>
</div>

<p>此接口是<a href="http://xmake.io/#/zh/manual?id=detect-has_cfuncs">detect.has_cfuncs</a>, <a href="http://xmake.io/#/zh/manual?id=detect-has_cincludes">detect.has_cincludes</a>和<a href="http://xmake.io/#/zh/manual?id=detect-has_ctypes">detect.has_ctypes</a>等接口的通用版本，也更加底层。</p>

<p>因此我们可以用它来检测：types, functions, includes 还有 links，或者是组合起来一起检测。</p>

<p>第一个参数为代码片段列表，一般用于一些自定义特性的检测，如果为空，则可以仅仅检测可选参数中条件，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="kd">local</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">check_cxsnippets</span><span class="p">({},</span> <span class="p">{</span><span class="n">types</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"wchar_t"</span><span class="p">,</span> <span class="s2">"char*"</span><span class="p">},</span> <span class="n">includes</span> <span class="o">=</span> <span class="s2">"stdio.h"</span><span class="p">,</span> <span class="n">funcs</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"sigsetjmp"</span><span class="p">,</span> <span class="s2">"sigsetjmp((void*)0, 0)"</span><span class="p">}})</span>
</code></pre>
</div>

<p>上面那个调用，会去同时检测types, includes和funcs是否都满足，如果通过返回true。</p>

<h4 id="更加强大的xmake-lua插件">更加强大的xmake lua插件</h4>

<p>2.1.4版本的时候，此插件就已经支持REPL(read-eval-print)，实现交互式运行来方便测试模块：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake lua
<span class="gp">&gt; </span>1 + 2
3

<span class="gp">&gt; </span>a <span class="o">=</span> 1
<span class="gp">&gt; </span>a
1

<span class="gp">&gt; </span><span class="k">for </span>_, v <span class="k">in </span>pairs<span class="o">({</span>1, 2, 3<span class="o">})</span> <span class="k">do</span>
<span class="gp">&gt;&gt; </span>print<span class="o">(</span>v<span class="o">)</span>
<span class="gp">&gt;&gt; </span>end
1
2
3
</code></pre>
</div>

<p>现在可以通过一行命令，更加快速地测试模块接口：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake lua lib.detect.find_package openssl
</code></pre>
</div>

<p>返回结果如下：<code class="highlighter-rouge"><span class="p">{</span><span class="err">links</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">{</span><span class="nt">"ssl"</span><span class="err">,</span><span class="w"> </span><span class="nt">"crypto"</span><span class="err">,</span><span class="w"> </span><span class="nt">"z"</span><span class="err">},</span><span class="w"> </span><span class="err">linkdirs</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">{</span><span class="nt">"/usr/local/lib"</span><span class="err">},</span><span class="w"> </span><span class="err">includedirs</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">{</span><span class="nt">"/usr/local/include"</span><span class="err">}}</span></code></p>

<h4 id="预编译头文件支持">预编译头文件支持</h4>

<p>xmake新增通过预编译头文件去加速<code class="highlighter-rouge">c/c++</code>程序编译，目前支持的编译器有：gcc, clang和msvc。</p>

<p>使用方式如下：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_precompiled_header</span><span class="p">(</span><span class="s2">"header.h"</span><span class="p">)</span>
</code></pre>
</div>

<p>通常情况下，设置c头文件的预编译，这需要加上这个配置即可，如果是对c++头文件的预编译，改成：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_precompiled_header</span><span class="p">(</span><span class="s2">"header.hpp"</span><span class="p">)</span>
</code></pre>
</div>

<p>其中的参数指定的是需要预编译的头文件路径，相对于当前<code class="highlighter-rouge">xmake.lua</code>所在的目录。</p>

<p>如果只是调用xmake命令行进行直接编译，那么上面的设置足够了，并且已经对各个编译器进行支持，但是有些情况下，上面的设置还不能满足需求：</p>

<ol>
  <li>如果要使用<code class="highlighter-rouge">xmake project</code>工程插件生成vs工程文件，那么还缺少一个类似<code class="highlighter-rouge">stdafx.cpp</code>的文件（上面的设置在msvc编译的时候会自动生成一个临时的，但是对IDE工程不友好）。</li>
  <li>如果gcc/clang下，<code class="highlighter-rouge">header.h</code>想作为c++的预编译头文件就不支持了，除非改成<code class="highlighter-rouge">header.hpp</code>（默认会当做c头文件进行预编译）。</li>
</ol>

<p>因此为了更加地通用跨平台，可以在工程里面创建一个类似vc中<code class="highlighter-rouge">stdafx.cpp</code>的源文件：<code class="highlighter-rouge">header.cpp</code>。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_precompiled_header</span><span class="p">(</span><span class="s2">"header.h"</span><span class="p">,</span> <span class="s2">"header.cpp"</span><span class="p">)</span>
</code></pre>
</div>

<p><code class="highlighter-rouge">header.cpp</code>的内容如下：</p>

<div class="language-cpp highlighter-rouge"><pre class="highlight"><code><span class="cp">#include "header.h"
</span></code></pre>
</div>

<p>上面的设置，就可以很好地处理各种情况下的预编译处理，追加的<code class="highlighter-rouge">header.cpp</code>也告诉了xmake：<code class="highlighter-rouge">header.h</code>是作为c++来预编译的。</p>

<p>相对于经典的vc工程中的<code class="highlighter-rouge">stdafx.cpp</code>和<code class="highlighter-rouge">stdafx.h</code>，也能完美支持：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_precompiled_header</span><span class="p">(</span><span class="s2">"stdafx.h"</span><span class="p">,</span> <span class="s2">"stdafx.cpp"</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="生成compiler_commands插件">生成compiler_commands插件</h4>

<p>扩展<code class="highlighter-rouge">xmake project</code>工程生成插件，支持<code class="highlighter-rouge">compiler_commands.json</code>文件输出，用于导出每个源文件的编译信息，生成基于clang的编译数据库文件，json格式，可用于跟ide，编辑器，静态分析工具进行交互。</p>

<div class="language-console highlighter-rouge"><pre class="highlight"><code><span class="w">$ </span><span class="nc">xmake</span><span class="kv"> project -k compile_commands
</span></code></pre>
</div>

<p>输出的内容格式如下：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>[
  { "directory": "/home/user/llvm/build",
    "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc",
    "file": "file.cc" },
  ...
]

</code></pre>
</div>

<p>一般用于跟IDE、编辑器插件、静态分析工具进行集成，对于<code class="highlighter-rouge">compile_commands</code>的详细说明见：<a href="#https://clang.llvm.org/docs/JSONCompilationDatabase.html">JSONCompilationDatabase</a></p>

<h4 id="自定义选项检测脚本">自定义选项检测脚本</h4>

<p>在选项检测之前，动态增加一些配置条件：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">option</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">)</span>
    <span class="n">before_check</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">option</span><span class="p">)</span>
        <span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.find_package"</span><span class="p">)</span>
        <span class="n">option</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="n">find_package</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">))</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>通过覆写检测脚本，控制选项的检测结果：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">option</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">add_deps</span><span class="p">(</span><span class="s2">"small"</span><span class="p">)</span>
    <span class="n">set_default</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
    <span class="n">on_check</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">option</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">option</span><span class="p">:</span><span class="n">dep</span><span class="p">(</span><span class="s2">"small"</span><span class="p">):</span><span class="n">enabled</span><span class="p">()</span> <span class="k">then</span>
            <span class="n">option</span><span class="p">:</span><span class="n">enable</span><span class="p">(</span><span class="kc">false</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果test依赖的选项通过，则禁用test选项。</p>

<p>在选项检测完成后，执行此脚本做一些后期处理，也可以在此时重新禁用选项：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">option</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">add_deps</span><span class="p">(</span><span class="s2">"small"</span><span class="p">)</span>
    <span class="n">add_links</span><span class="p">(</span><span class="s2">"pthread"</span><span class="p">)</span>
    <span class="n">after_check</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">option</span><span class="p">)</span>
        <span class="n">option</span><span class="p">:</span><span class="n">enable</span><span class="p">(</span><span class="kc">false</span><span class="p">)</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="自定义目标加载脚本">自定义目标加载脚本</h4>

<p>在target初始化加载的时候，将会执行<a href="http://xmake.io/#/zh/manual?id=targeton_load">on_load</a>，在里面可以做一些动态的目标配置，实现更灵活的目标描述定义，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="n">target</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"defines"</span><span class="p">,</span> <span class="s2">"DEBUG"</span><span class="p">,</span> <span class="s2">"TEST=\"</span><span class="n">hello</span><span class="err">\</span><span class="s2">""</span><span class="p">)</span>
        <span class="n">target</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"linkdirs"</span><span class="p">,</span> <span class="s2">"/usr/lib"</span><span class="p">,</span> <span class="s2">"/usr/local/lib"</span><span class="p">)</span>
        <span class="n">target</span><span class="p">:</span><span class="n">add</span><span class="p">({</span><span class="n">includedirs</span> <span class="o">=</span> <span class="s2">"/usr/include"</span><span class="p">,</span> <span class="s2">"links"</span> <span class="o">=</span> <span class="s2">"pthread"</span><span class="p">})</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>可以在<code class="highlighter-rouge">on_load</code>里面，通过<code class="highlighter-rouge">target:set</code>, <code class="highlighter-rouge">target:add</code> 来动态添加各种target属性。</p>

<h4 id="目标自定义构建脚本支持分平台架构">目标自定义构建脚本支持分平台架构</h4>

<p>通过设置<code class="highlighter-rouge">平台|架构</code>参数，控制自定义脚本的执行条件，实现在不同平台、架构下，调用不同的脚本进行构建：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">on_build</span><span class="p">(</span><span class="s2">"iphoneos|arm*"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span> 
        <span class="c1">-- TODO</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>或者对所有macosx平台，执行脚本：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">after_build</span><span class="p">(</span><span class="s2">"macosx"</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span> 
        <span class="c1">-- TODO</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>其他脚本，例如：<code class="highlighter-rouge">on_clean</code>, <code class="highlighter-rouge">before_package</code>等也都是支持的哦，而在2.1.4之前，只支持：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">on_package</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span> 
        <span class="c1">-- TODO</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>并不能对不同架构、平台分别处理。</p>

<h4 id="获取内置变量的值">获取内置变量的值</h4>

<p><a href="http://xmake.io/#/zh/manual?id=%E5%86%85%E7%BD%AE%E5%8F%98%E9%87%8F">内置变量</a>可以通过此接口直接获取，而不需要再加<code class="highlighter-rouge">$()</code>的包裹，使用更加简单，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="nb">print</span><span class="p">(</span><span class="n">val</span><span class="p">(</span><span class="s2">"host"</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">val</span><span class="p">(</span><span class="s2">"env PATH"</span><span class="p">))</span>
<span class="kd">local</span> <span class="n">s</span> <span class="o">=</span> <span class="n">val</span><span class="p">(</span><span class="s2">"shell echo hello"</span><span class="p">)</span>
</code></pre>
</div>

<p>而用<a href="http://xmake.io/#/zh/manual?id=vformat">vformat</a>就比较繁琐了：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="kd">local</span> <span class="n">s</span> <span class="o">=</span> <span class="n">vformat</span><span class="p">(</span><span class="s2">"$(shell echo hello)"</span><span class="p">)</span>
</code></pre>
</div>

<p>不过<code class="highlighter-rouge">vformat</code>支持字符串参数格式化，更加强大，所以应用场景不同。</p>

<h4 id="目标依赖实现属性继承">目标依赖实现属性继承</h4>

<p>2.1.4之前的版本，<a href="http://xmake.io/#/zh/manual?id=targetadd_deps">target.add_deps</a>仅用于添加依赖，修改编译顺序：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test1"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"static"</span><span class="p">)</span>
    <span class="n">set_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"test2"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"static"</span><span class="p">)</span>
    <span class="n">set_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"demo"</span><span class="p">)</span>
    <span class="n">add_deps</span><span class="p">(</span><span class="s2">"test1"</span><span class="p">,</span> <span class="s2">"test2"</span><span class="p">)</span>
    <span class="n">add_links</span><span class="p">(</span><span class="s2">"test1"</span><span class="p">,</span> <span class="s2">"test2"</span><span class="p">)</span>
    <span class="n">add_linkdirs</span><span class="p">(</span><span class="s2">"test1dir"</span><span class="p">,</span> <span class="s2">"test2dir"</span><span class="p">)</span>
</code></pre>
</div>

<p>2.1.5版本后，target还会自动继承依赖目标中的配置和属性，不再需要额外调用<code class="highlighter-rouge">add_links</code>, <code class="highlighter-rouge">add_includedirs</code>和<code class="highlighter-rouge">add_linkdirs</code>等接口去关联依赖目标了，上述代码可简化为：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test1"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"static"</span><span class="p">)</span>
    <span class="n">set_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"test2"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"static"</span><span class="p">)</span>
    <span class="n">set_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"demo"</span><span class="p">)</span>
    <span class="n">add_deps</span><span class="p">(</span><span class="s2">"test1"</span><span class="p">,</span> <span class="s2">"test2"</span><span class="p">)</span> <span class="c1">-- 会自动链接依赖目标</span>
</code></pre>
</div>

<p>并且继承关系是支持级联的，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"library1"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"static"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>
    <span class="n">add_headers</span><span class="p">(</span><span class="s2">"inc1/*.h"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"library2"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"static"</span><span class="p">)</span>
    <span class="n">add_deps</span><span class="p">(</span><span class="s2">"library1"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">)</span>
    <span class="n">add_headers</span><span class="p">(</span><span class="s2">"inc2/*.h"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_deps</span><span class="p">(</span><span class="s2">"library2"</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="新增查找工具接口">新增查找工具接口</h4>

<p><a href="http://xmake.io/#/zh/manual?id=detect-find_tool">lib.detect.find_tool</a>接口用于查找可执行程序，比<a href="http://xmake.io/#/zh/manual?id=detect-find_program">lib.detect.find_program</a>更加的高级，功能也更加强大，它对可执行程序进行了封装，提供了工具这个概念：</p>

<ul>
  <li>toolname: 工具名，可执行程序的简称，用于标示某个工具，例如：<code class="highlighter-rouge">gcc</code>, <code class="highlighter-rouge">clang</code>等</li>
  <li>program: 可执行程序命令，例如：<code class="highlighter-rouge">xcrun -sdk macosx clang</code></li>
</ul>

<p><code class="highlighter-rouge">lib.detect.find_program</code>只能通过传入的原始program命令或路径，去判断该程序是否存在。
而<code class="highlighter-rouge">find_tool</code>则可以通过更加一致的toolname去查找工具，并且返回对应的program完整命令路径，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">import</span><span class="p">(</span><span class="s2">"lib.detect.find_tool"</span><span class="p">)</span>

<span class="kd">local</span> <span class="n">tool</span> <span class="o">=</span> <span class="n">find_tool</span><span class="p">(</span><span class="s2">"clang"</span><span class="p">)</span>
</code></pre>
</div>

<p>我们也可以指定<code class="highlighter-rouge"><span class="p">{</span><span class="err">version</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">true</span><span class="p">}</span></code>参数去获取工具的版本，并且指定一个自定义的搜索路径，也支持内建变量和自定义脚本哦：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="kd">local</span> <span class="n">tool</span> <span class="o">=</span> <span class="n">find_tool</span><span class="p">(</span><span class="s2">"clang"</span><span class="p">,</span> <span class="p">{</span><span class="n">check</span> <span class="o">=</span> <span class="s2">"--help"</span><span class="p">})</span> 
<span class="kd">local</span> <span class="n">tool</span> <span class="o">=</span> <span class="n">find_tool</span><span class="p">(</span><span class="s2">"clang"</span><span class="p">,</span> <span class="p">{</span><span class="n">check</span> <span class="o">=</span> <span class="k">function</span> <span class="p">(</span><span class="n">tool</span><span class="p">)</span> <span class="n">os</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="s2">"%s -h"</span><span class="p">,</span> <span class="n">tool</span><span class="p">)</span> <span class="k">end</span><span class="p">})</span>
<span class="kd">local</span> <span class="n">tool</span> <span class="o">=</span> <span class="n">find_tool</span><span class="p">(</span><span class="s2">"clang"</span><span class="p">,</span> <span class="p">{</span><span class="n">version</span> <span class="o">=</span> <span class="kc">true</span><span class="p">,</span> <span class="p">{</span><span class="n">pathes</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"/usr/bin"</span><span class="p">,</span> <span class="s2">"/usr/local/bin"</span><span class="p">,</span> <span class="s2">"$(env PATH)"</span><span class="p">,</span> <span class="k">function</span> <span class="p">()</span> <span class="k">return</span> <span class="s2">"/usr/xxx/bin"</span> <span class="k">end</span><span class="p">}})</span>
</code></pre>
</div>

<p>最后总结下，<code class="highlighter-rouge">find_tool</code>的查找流程：</p>

<ol>
  <li>优先通过<code class="highlighter-rouge"><span class="p">{</span><span class="err">program</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nt">"xxx"</span><span class="err">}</span></code>的参数来尝试运行和检测。</li>
  <li>如果在<code class="highlighter-rouge">xmake/modules/detect/tools</code>下存在<code class="highlighter-rouge">detect.tools.find_xxx</code>脚本，则调用此脚本进行更加精准的检测。</li>
  <li>尝试从<code class="highlighter-rouge">/usr/bin</code>，<code class="highlighter-rouge">/usr/local/bin</code>等系统目录进行检测。</li>
</ol>

<p>我们也可以在工程<code class="highlighter-rouge">xmake.lua</code>中<code class="highlighter-rouge">add_moduledirs</code>指定的模块目录中，添加自定义查找脚本，来改进检测机制：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>projectdir
  - xmake/modules
    - detect/tools/find_xxx.lua
</code></pre>
</div>

<h4 id="更加安全的root权限编译">更加安全的root权限编译</h4>

<p>由于xmake提供强大的自定义模块和脚本支持，并且内置安装、卸载等action，如果<code class="highlighter-rouge">xmake.lua</code>里面的脚本描述不当，容易导致覆写系统文件，因此新版本对此作了改进：</p>

<ol>
  <li>在root下编译工程，先判断工程目录的用户权限属性，尝试降权到非root用户进行编译。</li>
  <li>如果需要写一些系统文件，会提示用户当前权限不安全，禁止继续运行，除非加<code class="highlighter-rouge">--root</code>参数强制root运行。</li>
  <li>如果当期工程目录是root用户权限，则同2。</li>
</ol>

<p>具体详情见：<a href="https://github.com/xmake-io/xmake/pull/113">pull#113</a></p>

<h4 id="api接口改进">API接口改进</h4>

<p>使用<a href="http://xmake.io/#/zh/manual?id=includes">includes</a>替代老的<a href="http://xmake.io/#/zh/manual?id=add_subdirs">add_subdirs</a>和<a href="http://xmake.io/#/zh/manual?id=add_subfiles">add_subfiles</a>接口。
使用<a href="http://xmake.io/#/zh/manual?id=targetset_config_header">set_config_header</a>替代老的<a href="http://xmake.io/#/zh/manual?id=targetset_config_h">set_config_h</a>和<a href="http://xmake.io/#/zh/manual?id=targetset_config_h_prefix">set_config_h_prefix</a>接口。</p>

<h4 id="新增大量扩展模块">新增大量扩展模块</h4>

<ul>
  <li>文件下载</li>
  <li>解压缩</li>
  <li>git操作等接口</li>
</ul>

<p>具体详情见文档：<a href="http://xmake.io/#/zh/manual?id=%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97">扩展模块</a></p>


        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
                    
                    <h2 id="english">English</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/2017/07/29/new-features-v2.1.5/">Some new features of xmake v2.1.5
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        
            </ul>
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2017/05/10/update-v2.1.4/">xmake v2.1.4版本更新，增加交互式命令执行(REPL)</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2017/07/31/precompiled-header/">不同编译器对预编译头文件的处理</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
